/*
  学习目标：分离reducer和初始值
  1. 将reducer提取为单独的函数
  2. 使用switch，替换if语句
  3. 提取变量，作为初始状态
*/

import { createStore } from 'redux';

// 3. 提取一个变量作为初始state
const initState = {
    count: 100,
    msg: 'Hello React',
};

// 1. 声明reducer
function reducer(state = initState, action) {
    console.warn('进到仓库了, action是  ----->', action);
    // console.log(state.count, action.palyload);
    // 2. 使用switch与替换if语句
    switch (action.type) {
        case 'add':
            return { ...state, count: state.count + action.payload };

        case 'des':
            return { ...state, count: state.count - action.payload };

        default:
            return state;
    }
}

const store = createStore(reducer);

export default store;